<template>
    <div class="sortmanage" id="tree">
        <div class="btn-group">
            <button class="btn" @click='addGroup'>添加一级分组</button>
            <button class="btn">重命名</button>
            <button class="btn">保存</button>
        </div>
        <div class="tree-wrap">
          <el-tree
            :data="data5"
            node-key="id"
            class="tree"            
            :expand-on-click-node="false">
            <span class="custom-tree-node" slot-scope="{ node, data }">
              <span class="node-name">{{ node.label }}</span>
              <span>
                <span class="operation-add"   @click="() => append(data)"> 添加子类</span>
                <span class="operation-delete" @click="() => remove(node, data)">X</span>
                <span class="operation-text">
                  问题管理
                </span>
              </span>
            </span>
          </el-tree>
        </div>
    </div>
</template>
<script>
  let id = 1000;
export default {
 
    data() {
      const data = [
        {
          id: 1,
          label: '销售系统',
          children: [{
            id: 4,
            label: '二级 1-1',
            children: [{
              id: 9,
              label: '三级 1-1-1'
            }, {
              id: 10,
              label: '三级 1-1-2'
            }]
          }]
        }, 
        {
          id: 2,
          label: '销售系统',
          children: [{
            id: 5,
            label: '二级 2-1'
          }, {
            id: 6,
            label: '二级 2-2'
          }]
        },
       {
        id: 3,
        label: '销售系统',
        children: [
          {
            id: 7,
            label: '二级 3-1'
          },
          {
            id: 8,
            label: '二级 3-2'
          }
        ]
      }];
      return {
        data:data
      }
    },
    computed:{
      data5(){
        return JSON.parse(JSON.stringify(this.data))
      }

    },
    methods: {
      append(data) {
        const newChild = { id: id++, label: '新增标签', children: [] };
        if (!data.children) {
          this.$set(data, 'children', []);
        }
        data.children.push(newChild);
      },

      remove(node, data) {
        const parent = node.parent;
        console.log(parent);
        if(!node.parent.parent){  //如果点击的是一级分组的删除的话
          let id=node.data.id;
          let index=this.data.findIndex((item)=> {
            return id === item.id
          })
          this.data.splice(index,1);
        }
        const children = parent.data.children || parent.data;
        const index = children.findIndex(d => d.id === data.id);
        children.splice(index, 1);
        
      },
      addGroup(){  //添加一级分组
          this.data.push(
            {
              id:id++,
              label:'新增分组',
              children:[]
            }
          )
      }


    }
    
}
</script>
<style  lang='scss'>
.sortmanage{
   .btn-group{
      height: 44px;
      border-bottom: 1px solid #6f6f6f;
      font-size: 0;
      margin-bottom: 20px;
      .btn{
          padding: 2px 6px;
          margin-right: 12px;
          font-size: 16px;
          border: 1px solid #e1e1e1;
          background-color: #fff;
          border-radius: 3px;
          box-shadow: 0 0 1px rgba(0, 0, 0, .2);
          cursor: pointer;
      }

    }
    .tree-wrap{
      .tree{
        .custom-tree-node{
          span{
            display: inline-block;
          }
          .node-name{
            width: 400px;
          }
          .operation-add{
            width: 160px;
          }
          .operation-delete{
            width: 100px;
            color:#F30606;
          }
          .operation-text{
            width: 100px;
            color: #46DEF4;

          }

        }

      }

    }

}
   

</style>
